package com.ruoyi.oar.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.mongo.MongoUtils;
import com.ruoyi.oar.api.util.BaiduAiIdCardUtil;
import com.ruoyi.oar.domain.OarCheckInRecord;
import com.ruoyi.oar.domain.dto.OarCheckInRecordDTO;
import com.ruoyi.oar.domain.query.OarCheckInRecordQuery;
import com.ruoyi.oar.mapper.OarCheckInRecordMapper;
import com.ruoyi.oar.service.OarCheckInRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 租客入住记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-21
 */
@Slf4j
@Service
public class OarCheckInRecordServiceImpl implements OarCheckInRecordService
{
    @Autowired
    private OarCheckInRecordMapper oarCheckInRecordMapper;

    /**
     * 查询租客入住记录
     * 
     * @param id 租客入住记录主键
     * @return 租客入住记录
     */
    @Override
    public Optional<OarCheckInRecordDTO> selectOarCheckInRecordById(String id) {
        return oarCheckInRecordMapper.selectOarCheckInRecordById(id).map(oarCheckInRecord -> toDto(oarCheckInRecord,true));
    }

    /**
     * 查询租客入住记录列表
     * 
     * @param oarCheckInRecordQuery 租客入住记录
     * @return 租客入住记录
     */
    @Override
    public List<OarCheckInRecordDTO> selectOarCheckInRecordList(OarCheckInRecordQuery oarCheckInRecordQuery) {
        return oarCheckInRecordMapper.selectOarCheckInRecordList(oarCheckInRecordQuery).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 分页查询租客入住记录列表
     * @param oarCheckInRecordQuery 租客入住记录
     * @return
     */
    @Override
    public TableDataInfo<OarCheckInRecordDTO> findAll(OarCheckInRecordQuery oarCheckInRecordQuery) {
        List<OarCheckInRecord> list = oarCheckInRecordMapper.selectOarCheckInRecordList(oarCheckInRecordQuery);
        List<OarCheckInRecordDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }
    /**
     * 新增租客入住记录
     * 
     * @param oarCheckInRecordDTO 租客入住记录
     * @return 结果
     */
    @Override
    public String insertOarCheckInRecord(OarCheckInRecordDTO oarCheckInRecordDTO) {
        checkOarCheckInRecord(oarCheckInRecordDTO);
        //保存数据库
        OarCheckInRecord oarCheckInRecord =toEntity(oarCheckInRecordDTO);
        oarCheckInRecordMapper.insertOarCheckInRecord(oarCheckInRecord);
        return oarCheckInRecord.getId();
    }

    /**
     * 修改租客入住记录
     * 
     * @param oarCheckInRecordDTO 租客入住记录
     * @return 结果
     */
    @Override
    public int updateOarCheckInRecord(OarCheckInRecordDTO oarCheckInRecordDTO) {
        checkOarCheckInRecord(oarCheckInRecordDTO);
        return oarCheckInRecordMapper.updateOarCheckInRecord(toEntity(oarCheckInRecordDTO));
    }

    /**
     * 批量删除租客入住记录
     * 
     * @param ids 需要删除的租客入住记录主键
     * @return 结果
     */
    @Override
    public int deleteOarCheckInRecordByIds(String[] ids) {
        return oarCheckInRecordMapper.deleteOarCheckInRecordByIds(ids);
    }

    /**
     * 删除租客入住记录信息
     * 
     * @param id 租客入住记录主键
     * @return 结果
     */
    @Override
    public int deleteOarCheckInRecordById(String id)
    {
        return oarCheckInRecordMapper.deleteOarCheckInRecordById(id);
    }

    /**
     * 分页查询租客入住记录列表
     * @param oarCheckInRecordQuery 租客入住记录
     * @return
     */
    @Override
    public TableDataInfo<OarCheckInRecordDTO> selOarRecordAndHouseVoList(OarCheckInRecordQuery oarCheckInRecordQuery) {
        //oarCheckInRecordQuery.setPhone(SM4Utils.encryptData_out(oarCheckInRecordQuery.getPhone()));
        List<OarCheckInRecord> list = oarCheckInRecordMapper.selOarRecordAndHouseVoList(oarCheckInRecordQuery);
        List<OarCheckInRecordDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }


    /**
     * 解析身份信息
     * @param imgBase64Str
     * @return
     */
    @Override
    public Map<String,String> getIdCardInfo(String imgBase64Str){
        Map<String, String> data = BaiduAiIdCardUtil.getIdCardInfo(imgBase64Str);
        String imgUrl = MongoUtils.addFile(imgBase64Str);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("gmsfzh",data.get("gmsfzh"));
        resultMap.put("xm",data.get("xm"));
        resultMap.put("imgUrl",imgUrl);
        return resultMap;
    }

    /**
    * 校验
    * @param dto
    */
    private void checkOarCheckInRecord(OarCheckInRecordDTO dto) {

    }

    private OarCheckInRecord toEntity(OarCheckInRecordDTO dto) {
        return BeanUtil.toBean(dto, OarCheckInRecord.class);
    }

    private OarCheckInRecordDTO toDto(OarCheckInRecord entity) {
        return toDto(entity, false);
    }
    /**
     * 转DTO
     *
     * @param entity
     * @param withDetail 是否查询其他信息
     * @return
     */
    private OarCheckInRecordDTO toDto(OarCheckInRecord entity, Boolean withDetail) {
            OarCheckInRecordDTO dto =  BeanUtil.toBean(entity, OarCheckInRecordDTO.class);
        if (withDetail) {

        }
        return dto;
    }


}
